package com.wrl.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

public class LogHandler implements InvocationHandler {
    Object target;

    public LogHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();

        Object result = method.invoke(target, args);

        after();

        return result;
    }

    /**
     * 调用方法之前执行
     */
    private void before() {
        System.out.println("log start time " + LocalDateTime.now());
    }

    /**
     * 调用方法之后执行
     */
    private void after() {
        System.out.println("log end time " + LocalDateTime.now());
    }
}
